package com.zlm.weather.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * @Author: 郑
 * @Date: Create in 2020-1-17.
 */
public class XmlBuilderUtil {
    public static Object xmlStrToObject(Class<?> clazz,String xmlStr) throws Exception{
        Object xmlObject = null;
        JAXBContext context = JAXBContext.newInstance(clazz);
        //xml转对象
        Unmarshaller unmarshaller = context.createUnmarshaller();
        StringReader stringReader = new StringReader(xmlStr);
        xmlObject = unmarshaller.unmarshal(stringReader);
        if(null != stringReader){
            stringReader.close();
        }
        return xmlObject;
    }
}
